/*
 * Copyright (c) 2023 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "platform/ohos/backend/rs_vulkan_context_client.h"
#include <set>
#include <dlfcn.h>
#include <vector>
#include "common/rs_optional_trace.h"
#include "platform/common/rs_log.h"
#include "render_context/memory_handler.h"
#include "unistd.h"
#include "vulkan/vulkan_ohos.h"

namespace OHOS {
namespace Rosen {
thread_local std::shared_ptr<Drawing::GPUContext> RsVulkanContextClient::drawingContextClient_ = nullptr;
RsVulkanContextClient::RsVulkanContextClient()
{
    rsVulkanInterface_.Init();
    rsVulkanInterface_.isClient_ = true;
    // Init drawingContext_ bind to backendContext
    drawingContextClient_ = rsVulkanInterface_.CreateDrawingContext();
}

RsVulkanContextClient& RsVulkanContextClient::GetSingleton()
{
    static RsVulkanContextClient singleton {};
    return singleton;
}

RsVulkanInterface& RsVulkanContextClient::GetRsVulkanInterface()
{
    return rsVulkanInterface_;
}

VKAPI_ATTR VkResult RsVulkanContextClient::HookedVkQueueSubmit(VkQueue queue, uint32_t submitCount,
    VkSubmitInfo* pSubmits, VkFence fence)
{
    RsVulkanInterface& vkInterface = RsVulkanContextClient::GetSingleton().GetRsVulkanInterface();
    std::lock_guard<std::mutex> lock(vkInterface.graphicsQueueMutex_);
    RS_LOGD("%{public}s queue", __func__);
    RS_OPTIONAL_TRACE_NAME_FMT("%s queue", __func__);
    return vkInterface.vkQueueSubmit(queue, submitCount, pSubmits, fence);
}

VKAPI_ATTR VkResult RsVulkanContextClient::HookedVkQueueSignalReleaseImageOHOS(VkQueue queue, uint32_t waitSemaphoreCount,
    const VkSemaphore* pWaitSemaphores, VkImage image, int32_t* pNativeFenceFd)
{
    RsVulkanInterface& vkInterface = RsVulkanContextClient::GetSingleton().GetRsVulkanInterface();
    std::lock_guard<std::mutex> lock(vkInterface.graphicsQueueMutex_);
    RS_LOGD("%{public}s queue", __func__);
    RS_OPTIONAL_TRACE_NAME_FMT("%s queue", __func__);
    return vkInterface.vkQueueSignalReleaseImageOHOS(queue, waitSemaphoreCount, pWaitSemaphores, image, pNativeFenceFd);
}

std::shared_ptr<Drawing::GPUContext> RsVulkanContextClient::CreateDrawingContext(bool independentContext)
{
    auto& drawingContext = drawingContextClient_;
    if (drawingContext != nullptr && !independentContext) {
        return drawingContext;
    }
    drawingContext = GetRsVulkanInterface().CreateDrawingContext(independentContext, false);
    return drawingContext;
}

std::shared_ptr<Drawing::GPUContext> RsVulkanContextClient::GetDrawingContext()
{
    auto& drawingContext = drawingContextClient_;
    if (drawingContext != nullptr) {
        return drawingContext;
    }
    drawingContext = GetRsVulkanInterface().CreateDrawingContext(false, false);
    return drawingContext;
}

void RsVulkanContextClient::ClearGrContext()
{
    GetDrawingContext()->PurgeUnlockedResources(true);
    GetDrawingContext()->ResetContext();
}
}
}
